type TabBarItem = {
  pagePath: string
  selectedIconPath: string
  iconPath: string
  text: string
}

class TabBarStore {
  private static instance: TabBarStore
  private selected = 0
  private color = '#000000'
  private selectedColor = '#DC143C'
  private list: TabBarItem[] = [
    {
      pagePath: 'pages/index/index',
      selectedIconPath: '../static/images/tabbar_home_on.png',
      iconPath: '../static/images/tabbar_home.png',
      text: '首页'
    },
    {
      pagePath: 'pages/cookbook/cookbook',
      selectedIconPath: '../static/images/logo.png',
      iconPath: '../static/images/logo.png',
      text: '点单'
    },
    {
      pagePath: 'pages/square/square',
      selectedIconPath: '../static/images/logo.png',
      iconPath: '../static/images/logo.png',
      text: '广场'
    },
    {
      pagePath: 'pages/order/order',
      selectedIconPath: '../static/images/logo.png',
      iconPath: '../static/images/logo.png',
      text: '订单'
    },
    {
      pagePath: 'pages/oneself/oneself',
      selectedIconPath: '../static/images/logo.png',
      iconPath: '../static/images/logo.png',
      text: '我的'
    },
    {
      pagePath: 'pages/videos/videos',
      selectedIconPath: '../static/images/logo.png',
      iconPath: '../static/images/logo.png',
      text: '视频'
    }
  ]

  private subscribers: Array<(selected: number) => void> = []

  private constructor() {
  }

  public static getInstance(): TabBarStore {
    if (!TabBarStore.instance) {
      TabBarStore.instance = new TabBarStore()
    }
    return TabBarStore.instance
  }

  public getState() {
    return {
      selected: this.selected,
      color: this.color,
      selectedColor: this.selectedColor,
      list: this.list
    }
  }

  public setSelected(index: number) {
    this.selected = index
    this.notifySubscribers()
  }

  public subscribe(callback: (selected: number) => void) {
    this.subscribers.push(callback)
    return () => {
      this.subscribers = this.subscribers.filter(sub => sub !== callback)
    }
  }

  private notifySubscribers() {
    this.subscribers.forEach(callback => callback(this.selected))
  }
}

export const tabBarStore = TabBarStore.getInstance()
